import * as companyApi from '@/api/company'

const state = {
  companyList: {
    rows: [],
    total: 0
  },
  Permission:{}
}

const mutations = {
  SET_COMPANY_LIST(state, newValue) {
    state.companyList = newValue
  },
  SET_PERMISSION_LIST(state, newValue) {
    state.Permission = newValue
  }
}

const actions = {
  async updateCompanyList({ commit }, params) {
    const res = await companyApi.getCompanyList(params)
    // console.log(res)
    commit('SET_COMPANY_LIST', res)
  },
  async updatePermissionList({ commit }) {
    const res = await companyApi.getPermissionList()
      commit('SET_PERMISSION_LIST', res)
  }
}
//处理分配权限数据
function dataTransfer(data, pid) {
  return data
    .filter(d => d.pid === pid)
    .map(d => ({
      ...d,
      children: dataTransfer(data, d.id)
    }))
}

const getters = {
  departmentTree(state) {
    return dataTransfer(state.Permission, '0')
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions,
  getters
}
